<?php
class EventViewModel extends ViewModel { 
    protected $uid= null;
    protected $viewFields = array( 
        'Event'=>array('_type'=>'LEFT',
                    'id'=>'id',
                    'author_id'=>'author_id',
                    'title'=>'title',
                    'location'=>'location',
                    'img'=>'img',
                    'type'=>'type',
                    'content'=>'content',
                    'time_start'=>'time_start',
                    'time_end'=>'time_end',
                    'stamp_modified'=>'stamp_modified',
                    'visibility'=>'visibility'
        ),
        'Account'=>array('_type'=>'LEFT',
                    '_on'=>'Event.author_id=Account.uid',
                    'role'=>'role',
                    'name'=>'author_name',
                    'state'=>'author_state',
                    //('CONCAT("'.__APP__.'/user/'.'", `uid`)')=>'author_profile',
                    
        ), 
        /*
        //用于获取
        'EventMark'=>array('_type'=>'LEFT',
                    '_on'=>'EventMark.eid=Event.id',
                    'wish'=>'`wish`',
                    'take'=>'`take`',
        ),*/
                    
        /*
        'EventMark'=>array('_type'=>'LEFT',
                    '_as'=>'Wish',
                    '_on'=>'Mark.eid=Event.id',
                    'cnt_wish'=>'COUNT(`wish`)',
        ),
        ),*/
    );
    public function EventViewModel($name='', $uid=null){
        $this->uid= $uid;
        if (isset($this->uid)){
            //关联event_mark表
            $this->viewFields['EventMark']= array(
                '_on'=>"EventMark.uid=$this->uid AND EventMark.eid=Event.id",
                'wish'=>'`wish`',
                'take'=>'`take`',
                'score'=>'`score`',
            );
        }
        parent::__construct($name);
    }
    
    /****
    * 附属物，获取所有活动类型
    */
    static public function getTypes(){
        return L('EVENT_TYPE');
    }
    protected function _initialize(){
        //$Event= M('Event');
        //$this->viewFields['Event']+= $Event->getDbFields();
        $this->order('`id` DESC');
        return true;
    }
    //回调，用于增加where
    protected function X_options_filter(&$options) {
        if (is_array($options['where'])){
            $options['where']= array_merge($options['where'], $this->map);
        } else{
            $options['where']= $this->map;
        }
    }
    public function search($map){
        static $FIELDS='id, author_id, title, img, type, time_start, visibility, uid, name, role, state';
        static $FIELDS=array('id', 'author_id', 'title', 'img', 'type', 'time_start', 
                            'visibility', 'author_name', 'role', 'author_state');
        return $this->field($FIELDS)->where($map)->order('`time_start` DESC')->select();
    }        
    
    protected function _after_select(&$resultSet,$options) {
        /*dump($resultSet);
        foreach($resultSet as &$result){
            //$this->_after_find($result);
        }*/
    }
    protected function _after_find(&$result, $options) {
        //* 文字修正
        $t= L('EVENT_TYPE');
        $result['type_name']= $t[$result['type']];
        /*
        //获取关注人数
        $map=array(
            'eid'=>$result['id'],
            'wish'=>EventMarkModel::C('MARK_WISH'),
        );
        $result['cnt_wish']= M('EventMark')->where($map)->count();
        $map=array(
            'eid'=>$result['id'],
            'take'=>EventMarkModel::C('MARK_TAKE'),
        );
        $result['cnt_take']= M('EventMark')->where($map)->count();*/
        
        return true;
    }
} 
?>
